

jQuery(function ($) {
    var user_info = JSON.parse(localStorage.getItem('user_info'));
    if (!user_info) {
        window.location.href = "/login/login.html";
    }
    var id = user_info.id;
    var url = window.location.search;
    if (!url) {
        return;
    }
    var values = url.split("=")[1];
    var imgArr = [];
    var order_id;
    var param = {
        'Order': {
            'id': values
        },
        '[]': {
            'OrderDelivery': {
                'order_id': values
            },
            'OrderDeliveryGoods[]': {
                'OrderDeliveryGoods': {
                    'delivery_id@': '[]/OrderDelivery/id',
                    'OrderGoods': {
                        'id@': '/goods_id'
                    }
                }
            }
        }
    };
    send_ajax('post', '/get', param, function (data) {
        if (data.code == 200) {
            var maxMoney = data.Order.total_amt;
            order_id = data.Order.id;
            var time = data.Order.create_time.split('.')[0];
            var orderSn = data.Order.order_sn;
            $('#time').text(time);
            $('#order_sn').text('订单号：' + orderSn);
            var orderGoods = [];
            var orderServiceGoods = [];
            var html = "";
            for (var i = 0; i < data["[]"].length; i++) {
                orderGoods.push(data["[]"][i]);
            }
            for (var n = 0; n < orderGoods.length; n++) {
                if (orderGoods[n]["OrderDeliveryGoods[]"]) {
                    for (var j = 0; j < orderGoods[n]["OrderDeliveryGoods[]"].length; j++) {
                        orderServiceGoods.push(orderGoods[n]["OrderDeliveryGoods[]"][j]);
                    }
                }
            }
            for (var l = 0; l < orderGoods.length; l++) {
                for (var t = 0; t < orderServiceGoods.length; t++) {
                    if (orderGoods[l].OrderDelivery.id == orderServiceGoods[t].delivery_id) {
                        orderServiceGoods[t].OrderGoods.receiver = orderGoods[l].OrderDelivery.receiver;
                    }
                }
            }
            for (var k = 0; k < orderServiceGoods.length; k++) {
                html += '  <tr class="tr-bd">\n                                <td class="order_msg">\n                                    <div class="goods_item clearfix">\n                                        <div class="goods_pic" data-goods-id="' + orderServiceGoods[k].OrderGoods.id + '">\n                                            <i class="b-img"  data-goods-id="' + orderServiceGoods[k].OrderGoods.id + '"></i>\n                                            <a href="/goods-' + orderServiceGoods[k].OrderGoods.id + '.html" target="_blank"><img src="' + (orderServiceGoods[k].OrderGoods.thumb ? isImgHttp(orderServiceGoods[k].OrderGoods.thumb) : '/asset/image/tu4.jpg') + '" alt="' + orderServiceGoods[k].OrderGoods.goods_name + '"></a>\n                                        </div>\n                                        <div class="goods_msg">\n                                            <div class="goods_name">\n                                                <a href="/goods-' + orderServiceGoods[k].OrderGoods.id + '.html" target="_blank">' + orderServiceGoods[k].OrderGoods.goods_name + '</a>\n                                            </div>\n                                        </div>\n                                    </div>\n                                </td>\n                                <td class="orderName">\n                                    <div class="txt">\n                                    ' + orderServiceGoods[k].OrderGoods.receiver + '<i></i>\n                                    </div>\n                                </td>\n                            </tr>\n                    ';
            }
            $('.myOrders').html(html);
            $('.b-img').click(function () {
                if ($(this).hasClass('b-c-img')) {
                    $(this).removeClass('b-c-img').addClass('b-img');
                } else {
                    $(this).addClass('b-c-img').removeClass('b-img');
                }
            });
            //输入金额的判断
            $('.refund_p').blur(function () {
                var value = $('.refund_p').val();
                if (value > maxMoney) {
                    alert('最多￥' + maxMoney);
                    $('.refund_p').val(maxMoney);
                }
            });
        }
        refundType();
    });
    //快递公司渲染
    logistics();
    function logistics() {
        var param = {
            "Logistics[]": {
                "Logistics": {
                    "use_flag": 1
                }
            }
        };
        send_ajax('post', '/get', param, function (data) {
            var html = "";
            if (data.code == 200&&data["Logistics[]"]) {
                var data = data["Logistics[]"];
                for (var i = 0; i < data.length; i++) {
                    html += '\n                    <option value="" data-id="' + data[i].id + '">' + data[i].name + '</option>';
                }
                $('.company').append(html);
            }
        });
    }
    $("input[type='radio']").click(function () {
        refundType();
    });
    //退款的方式
    function refundType() {
        var type = $("input[type='radio']:checked").val();
        if (type == 1) {
            $('.goods_pic i').hide();
            $('#logistics_company').hide();
            $('#delivery').hide();
        } else {
            $('.goods_pic i').show();
            $('#logistics_company').show();
            $('#delivery').show();
        }
    }
    //申请售后提交
    $('.btn-submin').click(function () {
        var order_goods_arr = [];
        var refundType = $("input[type='radio']:checked").val(); //退款的类型
        var refund_msg = $('.refund_msg').val(); //退款说明
        var refund_p = $('.refund_p').val(); //退款金额
        var delivery_sn = $('#delivery_sn').val(); //退款的返货单
        var el = $('.goods_pic i'); //选中的商品元素
        var logisticsId = $('#company option:selected').attr('data-id'); //快递公司选择
        for (var k = 0; k < el.length; k++) {

            if ($(el[k]).hasClass('b-c-img')) {

                order_goods_arr.push($(el[k]).attr('data-goods-id'));
            }
        }

        var param = {
            imgs: imgArr,
            money: refund_p,
            order_id: order_id,
            user_id: id,
            note: refund_msg
        };
        if (logisticsId != undefined) {
            param.logistics_id = logisticsId;
            param.delivery_sn = delivery_sn;
        }
        if (order_goods_arr.length) {
            param.order_goods_array = order_goods_arr;
        }

        send_ajax('post', '/api/order/order_back', param, function (data) {
            if (data.code == 200) {
                alert('申请提交成功');
                window.location.reload();
            }
        });
    });
    //提交的图片展示
    function img_show(param) {
        $('.img_box').append('<div class="img_list">\n                <img src="' + param + '" alt="">\n                <span class="de_img"><i class="deleteImg"></i></span>\n            </div>');
    }
    //图片上传
    $(".img_upload").click(function () {
        $(".file").click(); //隐藏了input:file样式后，点击头像就可以本地上传
        $(".file").on("change", function () {
            var $filePath = URL.createObjectURL($("input[name='file']")[0].files[0]);
            var formData = new FormData();
            formData.append("image", $("input[name='file']")[0].files[0]);
            $.ajax({
                url: host + "/api/upload/img/2",
                type: 'POST',
                data: formData,
                cache: false,
                // async: false,
                // // 告诉jQuery不要去处理发送的数据
                processData: false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType: false,
                // beforeSend: function () {
                // },
                success: function success(data) {
                    if (data.code == 200) {
                        imgArr.push($filePath);
                        $('.file').val(''); //解决不能同时上传同一张图片
                        // var img = document.createElement('img');
                        // img.src= $filePath;
                        img_show($filePath);

                        // $('.img_box').append(img);
                        if ($('.img_box img').length >= 3) {
                            $('.img_upload').hide();
                        }
                        $('.img_list').mouseenter(function () {
                            $(this).find('.de_img').show();
                        }).mouseleave(function () {
                            $(this).find('.de_img').hide();
                        });
                        $(".file").unbind();
                    } else {
                        alert('网络加载失败，请稍候再试!');
                    }
                },
                error: function error(data) {
                    alert('网络加载失败，请稍候再试!');
                }
            });
        });
    });
    //点击删除图片
    $(document).on('click', '.deleteImg', function () {
        var _thisParent = $(this).parents('.img_list').parent('.img_box');
        $(this).parents('.img_list').remove();
        var imgLength = _thisParent.find('.img_list').length;
        if (imgLength < 3) {
            _thisParent.parents('.choose_type').find('.img_upload').show();
        }
    });
});